package admin

import (
	"baokaobang/datasourse"
	"baokaobang/models"
	"baokaobang/utils"
	"encoding/json"
	"fmt"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"strings"
)

type AddRole struct {
	Id          string `json:"id"`
	Name        string `json:"name"`
	Authorities string `json:"authorities"`
}

type RoleListParam struct {
	PerPage int `json:"perPage"`
	CurPage int `json:"curPage"`
}

type Role struct {
	Ctx iris.Context
}

func (c *Role) OptionsCreate() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *Role) PostCreate() interface{} {
	addRole := AddRole{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接收请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &addRole)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错:%s", err.Error()), "")
	}

	if len(addRole.Name) <= 0 {
		return utils.CommonRes(1000, "请输入角色名称", "")
	}
	if len(addRole.Authorities) <= 0 {
		return utils.CommonRes(1000, "请至少选择一个权限", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库:%s", err.Error()), "")
	}

	// 基础权限
	baseAuthorities := []string{
		"adminuserinfo",
	}

	role := new(models.Role)
	role.Name = addRole.Name
	role.Authorities = fmt.Sprintf("%s,%s", addRole.Authorities, strings.Join(baseAuthorities, ","))
	_, err = db.InsertOne(role)

	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("操作失败:%s", err.Error()), "")
	}

	return utils.CommonRes(20000, "操作成功", "")
}

func (c *Role) OptionsEdit() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *Role) PostEdit() interface{} {
	addRole := AddRole{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接收请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &addRole)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错:%s", err.Error()), "")
	}

	if len(addRole.Id) <= 0 {
		return utils.CommonRes(1000, "请提供权限分类id", "")
	}
	if len(addRole.Name) <= 0 {
		return utils.CommonRes(1000, "请输入权限分类名称", "")
	}
	if len(addRole.Authorities) <= 0 {
		return utils.CommonRes(1000, "请至少选择一项权限", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库:%s", err.Error()), "")
	}

	// 基础权限
	baseAuthorities := []string{
		"adminuserinfo",
	}

	updateData := map[string]interface{}{
		"name":    addRole.Name,
		"authorities": fmt.Sprintf("%s,%s", addRole.Authorities, strings.Join(baseAuthorities, ",")),
	}

	_, err = db.Table(new(models.Role)).ID(addRole.Id).Update(updateData)
	if err != nil {
		return utils.CommonRes(1000, "操作失败", "")
	}

	return utils.CommonRes(20000, "操作成功", "")
}

func (c *Role) OptionsAll() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *Role) PostAll() interface{} {
	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库:%s", err.Error()), "")
	}

	roleList := make([]models.Role, 0)
	err = db.Find(&roleList)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法获取角色列表:%s", err.Error()), "")
	}

	return utils.CommonRes(20000, "成功获取角色列表", roleList)
}

func (c *Role) OptionsList() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *Role) PostList() interface{} {
	roleListParam := RoleListParam{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接收请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &roleListParam)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错:%s", err.Error()), "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库:%s", err.Error()), "")
	}

	start := (roleListParam.CurPage - 1) * roleListParam.PerPage
	roleList := make([]models.Role, 0)
	sess := db.Limit(roleListParam.PerPage, start)
	err = sess.Find(&roleList)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法获取角色列表:%s", err.Error()), "")
	}

	total, err := db.Where("id > ?", 0).Count(new(models.Role))
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法获取角色列表:%s", err.Error()), "")
	}

	for i, item := range roleList {
		roleList[i].RoleCreated = item.Created.Format("2006-01-02 15:04")
	}

	data := map[string]interface{}{
		"total": total,
		"list":  roleList,
	}

	return utils.CommonRes(20000, "成功获取角色列表", data)
}

func (c *Role) OptionsDetail() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *Role) PostDetail() interface{} {
	editParam := utils.EditParam{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接收请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &editParam)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错:%s", err.Error()), "")
	}
	if len(editParam.Id) <= 0 {
		return utils.CommonRes(1000, "参数不完整", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库:%s", err.Error()), "")
	}

	role := new(models.Role)
	ok, err := db.ID(editParam.Id).Get(role)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("信息获取失败:%s", err.Error()), "")
	}
	if !ok {
		return utils.CommonRes(1000, "信息获取失败", "")
	}

	return utils.CommonRes(20000, "信息获取成功", role)
}

func (c *Role) OptionsDel() mvc.Response {
	return mvc.Response{
		Code: 204,
	}
}

func (c *Role) PostDel() interface{} {
	delParam := utils.DelParam{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接收请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &delParam)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错:%s", err.Error()), "")
	}

	if len(delParam.Ids) <= 0 {
		return utils.CommonRes(1000, "请提供要删除项的id", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库:%s", err.Error()), "")
	}

	ids := strings.Split(delParam.Ids, ",")
	for _, id := range ids {
		role := new(models.Role)
		db.ID(id).Get(role)
		db.ID(id).Delete(role)
	}

	return utils.CommonRes(20000, "操作成功", "")
}
